#include <math.h>

#include "AnimatedSprite.h"
#include "GlobalUse.h"

VAnimatedSprite::VAnimatedSprite(void)
{
	VSprite::VSprite();
}

VAnimatedSprite::~VAnimatedSprite(void)
{
	VSprite::~VSprite();
}

VAnimatedSprite::VAnimatedSprite(CString strSpriteName)
{
	VBaseD3D* baseD3D = gfGetD3DBase();
	//Create sprite
	D3DXCreateTextureFromFileEx(baseD3D->d3ddev,		// the device pointer
                            strSpriteName,				// the new file name
                            D3DX_DEFAULT,				// default width
                            D3DX_DEFAULT,				// default height
                            D3DX_DEFAULT,				// no mip mapping
                            NULL,						// regular usage
                            D3DFMT_A8R8G8B8,			// 32-bit pixels with alpha
                            D3DPOOL_MANAGED,			// typical memory handling
                            D3DX_DEFAULT,				// no filtering
                            D3DX_DEFAULT,				// no mip filtering
                            D3DCOLOR_XRGB(255, 0, 255),	// the hot-pink color key
                            NULL,						// no image info struct
                            NULL,						// not using 256 colors
                            &m_sprite);					// load to sprite
	m_v3position = new VVector3(0.0f,0.0f,0.0f);
	m_v3anchor = new VVector3(0.0f, 0.0f, 0.0f);
	
	m_nStartX = 0;
	m_nStartY = 0;

	m_nWidth = 0;
	m_nHeight = 0;

	m_nMaximumAnim = 1;
	m_nCurrentAnim = 0;
	m_nMaximumTime = 1;
	m_nCurrentTime = 0;

	m_bRepeat = FALSE;
	StartAnimation();
}

void VAnimatedSprite::Draw() 
{
	if ((m_nWidth == 0) || (m_nHeight == 0)) {
		VSprite::Draw();
		return;
	}

	RECT rectDrawArea;
	int nDrawX = m_nStartX + m_nWidth * m_nCurrentAnim;
	
	SetRect(&rectDrawArea, nDrawX, m_nStartY, nDrawX + m_nWidth, m_nStartY + m_nHeight);
    
	VBaseD3D* baseD3D = gfGetD3DBase();
	// draw the sprite
    baseD3D->d3dspt->Draw(m_sprite, 
		&rectDrawArea,
		&m_v3anchor->GetPosition(), 
		&m_v3position->GetPosition(), 
		D3DCOLOR_XRGB(255, 255, 255));

	//Advance to next frame


	if (m_bRunning == TRUE) {
		m_nCurrentTime++;
		if (m_nCurrentTime >= m_nMaximumTime) {
			m_nCurrentTime = 0;
			if (m_nCurrentAnim+1 >= m_nMaximumAnim ) {
				if (m_bRepeat == TRUE) {
					m_nCurrentAnim = 0;
				}
			}
			else {
				m_nCurrentAnim++;
			}
		}
	}
}

void VAnimatedSprite::Release() 
{
	VSprite::Release();
}

//Set the sprite size
void VAnimatedSprite::SetDrawSize(int nWidth, int nHeight)
{
	m_nWidth = nWidth;
	m_nHeight = nHeight;
}
void VAnimatedSprite::SetDrawStart(int nStartX, int nStartY)
{
	m_nStartX = nStartX;
	m_nStartY = nStartY;
}

//Set the animation maximum amount
void VAnimatedSprite::SetMaximumAnimation(int nAnimSize) 
{
	m_nMaximumAnim = nAnimSize;
}

//Set the current animation
void VAnimatedSprite::SetCurrentAnimation(int nAnimCurrent)
{
	m_nCurrentAnim = nAnimCurrent;
}

//Set the maximum of time before next sprite
void VAnimatedSprite::SetMaximumTime(int nTimeMax)
{
	m_nMaximumTime = nTimeMax;
}

//Set the current time before next sprite
void VAnimatedSprite::SetCurrentTime(int nTimeCurrent)
{
	m_nCurrentTime	= nTimeCurrent;
}

void VAnimatedSprite::SetRunRepeat()
{
	m_bRepeat = TRUE;
}
void VAnimatedSprite::SetRunOnce()
{
	m_bRepeat = FALSE;
}
void VAnimatedSprite::StopAnimation()
{
	m_bRunning = FALSE;
}
void VAnimatedSprite::StartAnimation()
{
	m_bRunning = TRUE;
}
void VAnimatedSprite::RestartAnimation()
{
	m_bRunning = TRUE;
	m_nCurrentAnim = 0;
}

int VAnimatedSprite::GetMaximumAnimation() 
{
	return m_nMaximumAnim;
}

int VAnimatedSprite::GetCurrentAnimation() 
{
	return m_nCurrentAnim;
}